<?php
// +----------------------------------------------------------------------
// | 随访系统
// +----------------------------------------------------------------------
// | Copyright (c) 2018-2050 http://www.hedesoft.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +---------------------------------------------------------------------
// | Author: 小李子 < 490959289@qq.com>
// +----------------------------------------------------------------------
namespace app\common\model;

use think\Model;
use think\Db;
class User extends Model
{
	/**
     * 检查账号密码
     * @param $login_name 登录名称
     * @param $login_pwd 登录密码
     * @access public
     */
	public function login($login_name,$login_pwd)
	{
		$user = $this->where('phone',$login_name)->whereOr('uname',$login_name)->find();
		$login_pwd = md5($user['verify_code'].md5($login_pwd));
		if (!$user){
			return ['status'=>0,'msg'=>'账号不存在'];
		}
		if ($user['password']!=$login_pwd){
			return ['status'=>0,'msg'=>'登录密码错误'];
		}
		if ($user['status']==1){
			return ['status'=>0,'msg'=>'账号已被禁用'];
		}
		if ($user['is_del']==1){
			return ['status'=>0,'msg'=>'账号已被移除'];
		}
		return $this->loginToken($user['uid']);
	}
	/**
     * 注册
     * @param $login_name 登录名称
     * @param $login_pwd 登录密码
     * @access public
     */
	public function register($login_name,$login_pwd)
	{
		$user = $this->where('phone',$login_name)->whereOr('uname',$login_name)->find();
		if ($user){
			return ['status'=>0,'msg'=>'账号已存在'];
		}
		$verify_code = mt_rand();
		$login_pwd = md5($verify_code.md5($login_pwd));
		$data = [
			'verify_code'=>$verify_code,
			'uname'=>$login_name,
			'phone'=>$login_name,
			'password'=>$login_pwd,
			'ctime'=>time(),
			'last_time'=>time(),
		];
		$uid = $this->insertGetId($data);
		if (!$uid){
    		return ['status'=>0,'msg'=>'账号创建失败'];
    	}
		return $this->loginToken($uid);
	}
	/**
     * 修改用户密码
     * @param $uid 用户ID
     * @param $login_pwd 登录密码
     * @access public
     */
	public function updatePwd($uid,$login_pwd)
	{
		$verify_code = mt_rand();
		$login_pwd = md5($verify_code.md5($login_pwd));
		$data = [
			'verify_code'=>$verify_code,
			'password'=>$login_pwd,
		];
		$run = $this->where('uid',$uid)->update($data);
		if (!$run){
    		return ['status'=>0,'msg'=>'密码修改失败'];
    	}
		return ['status'=>1,'msg'=>'密码修改成功'];
	}
	/**
     * 登录及缓存
     * @param $login_name 登录名称
     * @param $login_pwd 登录密码
     * @access private
     */
	public function loginToken($uid)
	{
		$user = $this->where('uid',$uid)->find();
		$code = mt_rand();
		$token = md5(time().md5($code));
		cache('token_'.$token, $user);
		$token_data = [
    		'uid'=>$uid,
    		'token'=>$token,
    		'ctime'=>time(),
    	];
    	$run = Db::name('user_token')->insert($token_data);
    	if ($run){
    		return ['status'=>1,'msg'=>'登录成功','token'=>$token];
    	}else{
    		return ['status'=>0,'msg'=>'token生成失败'];
    	}
		
	}
}